package com.agentlab.imitation.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.agentlab.imitation.bean.TdYxzydmAll;
import com.agentlab.imitation.bean.Zy;
import com.agentlab.imitation.bean.Zydl;
import com.agentlab.imitation.service.base.DaoSupport;

@Service
@Transactional
public class YxzydmServiceBean extends DaoSupport<TdYxzydmAll> implements YxzydmService {
	@Resource
	SessionFactory factory;

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void delete(Serializable... entityids) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unchecked")
	public List<Zydl> getZydlList(String pcdm, String kldm) {
		// TODO Auto-generated method stub
		List<Zydl> zydlList = new ArrayList<Zydl>();
		List list = factory.getCurrentSession().createQuery(
		// " select distinct xkmldm,xkmlmc from TdYxzydmAll where kldmOld=:kldmOld and bzzydm in (select distinct bzzydm from TLqkAll where pcdm=:pcdm and kldm=:kldm ) ")
				// .setParameter("kldmOld", kldm).setParameter("pcdm",
				// pcdm).setParameter("kldm",
				// kldm).setCacheable(true).setCacheRegion("cache_Zydl_" +
				// pcdm+kldm).list();
				" select distinct xkmldm,xkmlmc from TdYxzydmAll where kldmOld=:kldmOld   ").setParameter("kldmOld", kldm).setCacheable(true).setCacheRegion("cache_Zydl_" + pcdm + kldm).list();
		// for (int i = 0; i < list.size(); i++) {
		// entity = (TdYxzydmAll) list.get(i);
		// zydlList.add(new Zydl(entity.getXkmldm(),entity.getXkmlmc()));
		// }
		for (Iterator it = list.iterator(); it.hasNext();) {
			Object[] d = (Object[]) it.next();
			// System.out.println(d[0]);
			// System.out.println(d[1]);
			zydlList.add(new Zydl(d[0].toString(), d[1].toString()));
		}

		return zydlList;
	}

	@SuppressWarnings("unchecked")
	public List<Zy> getZyList(String yjxkdm) {
		// TODO Auto-generated method stub
		TdYxzydmAll entity = null;
		List<Zy> zyList = new ArrayList<Zy>();
		// List list = factory
		// .getCurrentSession()
		// .createQuery(
		// " select distinct xkmldm,xkmlmc from TdYxzydmAll where bzkldm='11' or bzkldm = '15'  ")
		// .setCacheable(true).setCacheRegion("cache_Zydl").list();
		List list = factory
				.getCurrentSession()
				.createQuery(
						"select distinct xkmldm,xkmlmc,yjxkdm,yjxkmc,bzzydm,bzzymc  from TdYxzydmAll where (bzkldm='11' or bzkldm = '15') and yjxkdm = :yjxkdm")
				.setParameter("yjxkdm", yjxkdm).setCacheable(true).setCacheRegion("cache_Zy_" + yjxkdm).list();
		// for (int i = 0; i < list.size(); i++) {
		// entity = (TdYxzydmAll) list.get(i);
		// zyList.add(new Zy(entity.getXkmldm(),entity.getXkmlmc()));
		// }
		for (Iterator it = list.iterator(); it.hasNext();) {
			Object[] d = (Object[]) it.next();
			// System.out.println(d[0]);
			// System.out.println(d[1]);
			zyList.add(new Zy(d[0].toString(), d[1].toString(), d[2].toString(), d[3].toString(), d[4].toString(), d[5].toString()));
		}

		return zyList;
	}

	public List<Zydl> getYjxklist(String zydlid) {
		List<Zydl> yjxkList = new ArrayList<Zydl>();
		List list = factory.getCurrentSession().createQuery(
				" select distinct yjxkdm,yjxkmc from TdYxzydmAll where (bzkldm='11' or bzkldm = '15') and  xkmldm = :xkmldm  ").setParameter(
				"xkmldm", zydlid).setCacheable(true).setCacheRegion("cache_yjxk_" + zydlid).list();
		// for (int i = 0; i < list.size(); i++) {
		// entity = (TdYxzydmAll) list.get(i);
		// zydlList.add(new Zydl(entity.getXkmldm(),entity.getXkmlmc()));
		// }
		for (Iterator it = list.iterator(); it.hasNext();) {
			Object[] d = (Object[]) it.next();
			// System.out.println(d[0]);
			// System.out.println(d[1]);
			yjxkList.add(new Zydl(d[0].toString(), d[1].toString()));
		}

		return yjxkList;
	}
	
	/**
	 * 找到一级学科代码与名称
	 * @param bazydm
	 * @return
	 */
	public Zy getYjxkdm(String bzzydm)
	{
		Zy zy = new Zy("", "","","");
		List list = factory.getCurrentSession().createQuery(
				" select  yjxkdm,yjxkmc,bzzymc from TdYxzydmAll where bzzydm = :bzzydm  ").setParameter(
				"bzzydm", bzzydm).list();
		// for (int i = 0; i < list.size(); i++) {
		// entity = (TdYxzydmAll) list.get(i);
		// zydlList.add(new Zydl(entity.getXkmldm(),entity.getXkmlmc()));
		// }
		for (Iterator it = list.iterator(); it.hasNext();) {
			Object[] d = (Object[]) it.next();
			// System.out.println(d[0]);
			// System.out.println(d[1]);
			zy = new Zy(d[0].toString(), d[1].toString(),bzzydm,d[2].toString());
		}
		return zy;
	}

}
